package com.dozen.commonbase.mode;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;

import androidx.core.app.ActivityCompat;

import com.dozen.commonbase.APPBase;
import com.dozen.commonbase.utils.EmptyCheckUtil;
import com.dozen.commonbase.utils.MyLog;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * @author: Dozen
 * @description:
 * @time: 2021/7/8
 */
public class MyLocationUtil {
    private static String provider;

    public static Location getMyLocation() {
//        获取当前位置信息
        //获取定位服务
        LocationManager locationManager = (LocationManager) APPBase.getApplication().getSystemService(Context.LOCATION_SERVICE);
        //获取当前可用的位置控制器
        List<String> list = locationManager.getProviders(true);

        if (list.contains(LocationManager.NETWORK_PROVIDER)) {
//            网络位置控制器
            provider = LocationManager.NETWORK_PROVIDER;//网络定位
        } else if (list.contains(LocationManager.GPS_PROVIDER)) {
//            GPS位置控制器
            provider = LocationManager.GPS_PROVIDER;//GPS定位
        }

        if (provider != null) {
            if (ActivityCompat.checkSelfPermission(APPBase.getApplication(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(APPBase.getApplication(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return null;
            }
            Location location = locationManager.getLastKnownLocation(provider);// 调用getLastKnownLocation()方法获取当前的位置信息

            if (!EmptyCheckUtil.isEmpty(location)) {
                MyLog.d(location.getLongitude() + "__" + location.getLatitude());
            }
            MyLog.d("empty");

            return location;
        } else {
//            ToastUtils.makeText("请检查网络或GPS是否打开");
        }

        return null;
    }



    /**
     * 获取经纬度详细地址
     */
    public static String getLocationDetail(double latitude, double longitude, Context context) {

        StringBuilder location = new StringBuilder();

        Geocoder gc = new Geocoder(context, Locale.getDefault());
        List<Address> locationList = null;
        try {
            locationList = gc.getFromLocation(latitude, longitude, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Address address = locationList.get(0);//得到Address实例

        String countryName = address.getCountryName();//得到国家名称，比如：中国
        location.append(countryName).append("-");
        MyLog.i("countryName = " + countryName);

        String locality = address.getLocality();//得到城市名称，比如：北京市
        location.append(locality).append("-");
        MyLog.i("locality = " + locality);
        for (int i = 0; address.getAddressLine(i) != null; i++) {
            String addressLine = address.getAddressLine(i);//得到周边信息，包括街道等，i=0，得到街道名称
            if (address.getAddressLine(i + 1) != null) {
                location.append(addressLine).append("-");
            } else {
                location.append(addressLine);
            }

            MyLog.i("addressLine = " + addressLine);
        }

        return location.toString();
    }


    /**
     * 手机是否开启位置服务，如果没有开启那么所有app将不能使用定位功能
     */
    public static boolean isLocServiceEnable(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }
        return false;
    }
}
